<?php
/**
 * Created by PhpStorm.
 * User: 66diqiu-li
 * Date: 14-10-15
 * Time: 上午11:40
 */

define(URLPIC,'http://photo.66diqiu.com');

/**
 * 获取站点ID信息
 */
function get_site_id(){
    //获取路径
    $absolute_path = str_replace('\\', '/', getcwd()).'/';
    $absolute_path_data = explode('/',$absolute_path);
    for($i=0;$i<6;$i++){
        unset($absolute_path_data[count($absolute_path_data)-1]);
    }
    $absolute_path = implode('/',$absolute_path_data);
    $db = array();
    require_once($absolute_path.'/application/config/database.php');
    $database = $db['default'];
    $site_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
    $site_url .= "://".$_SERVER['HTTP_HOST'];

    $mysqliObj = new mysqli($database['hostname'],$database['username'],$database['password'],$database['database']);
    if(mysqli_connect_errno()){
        echo "连接失败".mysqli_connect_error();
        exit();
    }
    $mysqliObj->query("set name $database[char_set]");
    $sql = "SELECT sid FROM  ".$database['dbprefix']."siteinfo where url ='".$site_url."'";
    $result = $mysqliObj->query($sql);
    $data = array();
    while ($row = $result->fetch_assoc()){
        $data[] = array(
            'sid'=>$row['sid'],
        );
    }
    return $data[0]['sid'];
}

/**
 * 上传图片信息到ftp路径
 * @param $ftp_file
 */
function ftp_upload($ftp_file){
    include "Ftp.class.php";
    //获取路径
    $absolute_path = str_replace('\\', '/', getcwd()).'/';
    $absolute_path_data = explode('/',$absolute_path);
    for($i=0;$i<6;$i++){
        unset($absolute_path_data[count($absolute_path_data)-1]);
    }
    $absolute_path = implode('/',$absolute_path_data);

    //ftp获取
    $config = array();
    require_once($absolute_path.'/application/config/ftp.php');

    /*FTP 上传文件 start*/
    $ftp = new Ftp();
    $ftp->connect($config);
    $ftp_resource_path = substr($ftp_file,0,strrpos($ftp_file,'/')).'/';
    //创建文件夹
    $ftp->dmkdir($ftp_resource_path);
    //ftp上传
    $ftp->upload($absolute_path.$ftp_file, $ftp_file);
    if(@file_exists($absolute_path.$ftp_file)){
        @unlink($absolute_path.$ftp_file);
    }
    //关闭
    $ftp->close();
    /*FTP 上传文件 end*/
}

function get_ftp_list($path){
    include "Ftp.class.php";
    //获取路径
    $absolute_path = str_replace('\\', '/', getcwd()).'/';
    $absolute_path_data = explode('/',$absolute_path);
    for($i=0;$i<6;$i++){
        unset($absolute_path_data[count($absolute_path_data)-1]);
    }
    $absolute_path = implode('/',$absolute_path_data);

    //ftp获取
    $config = array();
    require_once($absolute_path.'/application/config/ftp.php');

    /*FTP 上传文件 start*/
    $ftp = new Ftp();
    $ftp->connect($config);
    $list = $ftp->filelist($path);

    //关闭
    $ftp->close();
    /*FTP 上传文件 end*/
    return $list;

}